# toolchain build order: linux-api-headers->glibc->binutils->gcc->binutils->glibc
# NOTE: valgrind requires rebuilt with each major glibc version

pkgname=glibc
pkgver=2.26
_commit=a5db85df69dce7fcf793fefc60c764c684e0c272
pkgrel=1
pkgdesc="GNU C Library"
arch=('x86_64')
url="http://www.gnu.org/software/libc"
license=('GPL' 'LGPL')
groups=('base')
depends=('linux-api-headers>=4.11' 'tzdata')
makedepends=('gcc>=6.3')
backup=(etc/gai.conf
        etc/locale.gen
        etc/nscd.conf)
options=('!strip')
install=glibc.install
#source=("http://ftp.gnu.org/gnu/libc/${pkgname}-${pkgver}.tar.xz"
source=("https://github.com/bminor/glibc/archive/${_commit}.zip"
        #"http://www.linuxfromscratch.org/patches/lfs/development/glibc-2.22-largefile-1.patch"
        'locale.gen.txt'
        'locale-gen'
        'glibc.conf')
md5sums=('7ea8253786d870ed4a71413700efbf62'
         #'203fe9133e82f2b59969c116b70fd84e'
         '07ac979b6ab5eeb778d55f041529d623'
         '476e9113489f93b348b21e144b6a8fcf'
         '8b3298de2392a10905a64ceccf56d2c1')

build() {
  cd ${srcdir}/${pkgname}-${_commit}
  #patch -p1 -i $srcdir/glibc-2.22-largefile-1.patch
  
  cd ${srcdir}
  mkdir glibc-build
  cd glibc-build

  echo "slibdir=/lib" >> configparms
  
  # remove stack protector for libs
  CFLAGS=${CFLAGS/-fstack-protector/}
  CPPFLAGS=${CPPFLAGS/-D_FORTIFY_SOURCE=2/}

  ${srcdir}/glibc-${_commit}/configure --prefix=/usr \
      --libdir=/usr/lib --libexecdir=/usr/lib \
      --with-headers=/usr/include \
      --with-bugurl=https://github.com/KaOSx/core/issues \
      --enable-add-ons \
      --enable-obsolete-rpc \
      --enable-obsolete-nsl \
      --enable-kernel=3.2.0 \
      --enable-bind-now --disable-profile \
      --enable-stackguard-randomization \
      --enable-lock-elision \
      --enable-multi-arch \
      --disable-werror

  # build libs without stack protector
  echo "build-programs=no" >> configparms
  make
  
  # build bins with stack protector
  sed -i "/build-programs=/s#no#yes#" configparms
  echo "CC += -fstack-protector -D_FORTIFY_SOURCE=2" >> configparms
  echo "CXX += -fstack-protector -D_FORTIFY_SOURCE=2" >> configparms
  make
}

check() {
  cd ${srcdir}/glibc-build
  
  # no stack protector for running tests
  sed -i '5,7d' configparms
  sed -i '/FORTIFY/d' configparms
  
  make check || true
}

package() {
  cd ${srcdir}/glibc-build

  install -dm755 ${pkgdir}/etc
  touch ${pkgdir}/etc/ld.so.conf

  make install_root=${pkgdir} install

  rm -f ${pkgdir}/etc/ld.so.{cache,conf}

  install -dm755 ${pkgdir}/{,usr/{sbin,lib/{,locale,systemd/system,tmpfiles.d}}}

  install -m644 ${srcdir}/glibc-${_commit}/nscd/nscd.conf ${pkgdir}/etc/nscd.conf
  install -m644 ${srcdir}/glibc-${_commit}/nscd/nscd.service ${pkgdir}/usr/lib/systemd/system
  install -m644 ${srcdir}/glibc-${_commit}/nscd/nscd.tmpfiles ${pkgdir}/usr/lib/tmpfiles.d/nscd.conf

  install -m755 ${srcdir}/locale-gen ${pkgdir}/usr/sbin
  install -m644 ${srcdir}/glibc-${_commit}/posix/gai.conf ${pkgdir}/etc/gai.conf

  # create /etc/locale.gen
  install -m644 ${srcdir}/locale.gen.txt ${pkgdir}/etc/locale.gen
  sed -e '1,3d' -e 's|/| |g' -e 's|\\| |g' -e 's|^|#|g' \
    ${srcdir}/glibc-${_commit}/localedata/SUPPORTED >> ${pkgdir}/etc/locale.gen

  # fix for the linker
  sed -i '/RTLDLIST/s%lib64%lib%' ${pkgdir}/usr/bin/ldd
  # Needed to set this? keep lib64 dir for now
  #mkdir ${pkgdir}/lib64
  cd ${pkgdir}/lib64
  ln -v -s ../lib/ld-2* .

  cd ${pkgdir}
  strip $STRIP_BINARIES sbin/{ldconfig,sln} \
                        usr/bin/{gencat,getconf,getent,iconv,locale} \
                        usr/bin/{localedef,pcprofiledump,rpcgen,sprof} \
                        usr/lib/getconf/* \
                        usr/sbin/{iconvconfig,nscd}

  #strip $STRIP_STATIC usr/lib/*.a 

  strip $STRIP_SHARED lib/{libanl,libBrokenLocale,libcidn,libcrypt}-*.so \
                      lib/libnss_{compat,dns,files,hesiod,nis,nisplus}-*.so \
                      lib/{libdl,libm,libnsl,libresolv,librt,libutil}-*.so \
                      lib/{libmemusage,libpcprofile,libSegFault}.so \
                      usr/lib/{audit,gconv}/*.so \
                      lib/libmvec-*.so

  # Still needed?  Add /usr/lib32 to the default library search path
  install -Dm644 ${srcdir}/glibc.conf ${pkgdir}/etc/ld.so.conf.d/glibc.conf
}
